Shader "Hidden/UpdateSlice"
{
    SubShader
    {
        Tags { "RenderType"="Opaque" "LightMode"="UniversalForward" }
        LOD 100

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 4.0
            
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

            struct Attributes
            {
                float4 positionOS : POSITION;
                float3 normalOS : NORMAL;
                float2 uv : TEXCOORD0;
            };

            struct Varings
            {
                float4 positionCS : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 positionWS : TEXCOORD1;
                float3 normalWS : TEXCOORD2;
                float3 viewDirWS : TEXCOORD3;
                float4 IVGIColor : TEXCOORD4;

            };

            
            TEXTURE3D(_MainTex);
            SAMPLER(sampler_MainTex);
            float4 _MainTex_ST;

            TEXTURE3D(_SourceTex);
            SAMPLER(sampler_SourceTex);
            float4 _SourceTex_ST;

            float _SliceIndex;
            float _Depth;
            
            Varings vert (Attributes IN)
            {
                Varings OUT;
                
                VertexPositionInputs positionInputs = GetVertexPositionInputs(IN.positionOS.xyz);
                VertexNormalInputs normalInputs = GetVertexNormalInputs(IN.normalOS.xyz);
                OUT.positionCS = TransformObjectToHClip(IN.positionOS);
                OUT.positionCS = positionInputs.positionCS;
                OUT.positionWS =  positionInputs.positionWS; 
                OUT.viewDirWS = GetCameraPositionWS() - positionInputs.positionWS;
                OUT.normalWS = normalInputs.normalWS;
                OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
                
                return OUT;
            }
            
            float4 frag (Varings IN) : SV_Target
            {
                 // 目标纹理当前的 z 切片位置
                float z = _SliceIndex / (_Depth - 1.0); // 注意要减1，避免 z=1 越界
                float3 uvw = float3(IN.uv.xy, z);
                float4 col = SAMPLE_TEXTURE3D_LOD(_SourceTex, sampler_SourceTex, uvw, 0).rgba;;
                return col;
            }
            ENDHLSL
        }
    }
}